#ifndef MODELSTATUSMANAGER_H
#define MODELSTATUSMANAGER_H
#include "cameraeth0.h"

#include <QObject>
#include <QString>
#include <QMutex>
#include <QMap>

class ModelStatusManager : public QObject {
    Q_OBJECT
    Q_PROPERTY(QMap<QString, QString> statuses READ statuses NOTIFY statusesChanged)
    Q_PROPERTY(QMap<QString, QString> errorMessages READ errorMessages NOTIFY errorMessagesChanged)

public:
    static ModelStatusManager& instance() {
        static ModelStatusManager instance;
        return instance;
    }

    QMap<QString, QString> statuses() const {
        QMutexLocker locker(&m_mutex);
        return m_statuses;
    }

    QMap<QString, QString> errorMessages() const {
        QMutexLocker locker(&m_mutex);
        return m_errorMessages;
    }
    void setCameraStatus(CameraEth0::CameraStatus status);
    CameraEth0::CameraStatus getCameraStatus() const {return m_cameraStatus;};

    void waitForCameraRunning();

public slots:
    void setError(const QString& module, const QString& status) {
        {
            QMutexLocker locker(&m_mutex);
            m_errors[module] = status;
        }
        //emit statusesChanged();
    }



    void setErrorMessage(const QString& module, const QString& errorMessage) {
        {
            QMutexLocker locker(&m_mutex);
            m_errorMessages[module] = errorMessage;
        }
        emit errorMessagesChanged();
    }

signals:
    void statusesChanged();
    void errorMessagesChanged();

private:
    ModelStatusManager(QObject *parent = nullptr) : QObject(parent) {}

    QMap<QString, QString> m_errors;
    QMap<QString, QString> m_statuses;
    QMap<QString, QString> m_errorMessages;

    CameraEth0::CameraStatus m_cameraStatus;
    mutable QMutex m_mutex;
    mutable std::mutex cameraMutex;
    std::condition_variable cameraCondition_;
};

#endif // MODELSTATUSMANAGER_H
